<?php
class ContentTag{
	//定义模板标签block为1则为块级标签，可以循环 level为可以嵌套的次数
	public $tag=array(
		'arclist'=>array('block'=>1,'level'=>4),
		'channel'=>array('block'=>1,'level'=>4),
		'pagelist'=>array('block'=>1,'level'=>4),
		'pageshow'=>array('block'=>0,'level'=>4)

	);

	/**
	 * 读取文章列表
	 * @param  [type] $attr    [description]
	 * @param  [type] $content [description]
	 * @return [type]          [description]
	 */
	public function _arclist($attr,$content){
		$cid = isset($attr['cid'])?$attr['cid']:''; //栏目id
		$aid = isset($attr['aid'])?$attr['aid']:''; //文章id
		$row = isset($attr['row'])?$attr['row']:10; //显示条数
		$titlelen = isset($attr['titlelen'])?$attr['titlelen']:20; //标题长度
		//输出PHP语法
		$php=<<<php
		<?php
		\$cid='$cid';
		\$aid='$aid';
		\$db = M('content');
		if(\$cid)\$db->where("cid in(\$cid)");
		if(\$aid)\$db->where("aid in(\$aid)");
		\$data = \$db->limit($row)->all();
		foreach(\$data as \$field):
			\$field['url']=U('Index/Index/content',array('aid'=>\$field['aid']));
			\$field['title']=mb_substr(\$field['title'],0,$titlelen,'utf-8');
		?>
php;
		$php.=$content;
		$php.='<?php endforeach;?>';
		return $php;
	}

	//读取栏目
	public function _channel($attr,$content){
		$cid=$attr['cid'];
		$type=isset($attr['type'])?$attr['type']:'self'; //同级栏目
		$php=<<<php
		<?php
		   \$cid=$cid;
		   \$type=$type;
		   \$db=M('category');

		   switch (\$type) {
		   	case 'self':
				\$cat= \$db->find($cid);
				\$data=\$db->where("pid=".\$cat['pid'])->all();
		   		break;
		   	
		   	case 'son':
				\$data=\$db->where("pid=".\$cid)->all();
		   		break;
		   }
		  foreach(\$data as \$field):
				\$field['caturl']=U('Index/Index/category',array('cid'=>\$field['cid']));
		 ?>	
php;
	$php.=$content;
	$php.='<?php endforeach; ?>';
	return $php;
	}

	//分页列表数据
	public function _pagelist($attr,$content){
		$row=isset($attr['row'])?$attr['row']:10; //显示条数
		$titlelen=isset($attr['titlelen'])?$attr['titlelen']:20; //标题长度
		$php=<<<php
<?php
	\$cid=\$_GET['cid'];
	//取所有子栏目
	\$cat = Data::channelList(M('category')->all(),\$cid);
	\$cat=array_keys(\$cat);//所有子栏目cid
	\$cat[]=\$cid;//当前栏目加入
	\$db= M('content');
	\$where['cid']=array('IN',\$cat);
	\$page = new Page(\$db->where(\$where)->count(),$row);
	\$data= \$db->where(\$where)->limit(\$page->limit())->all();
	foreach(\$data as \$field):
		\$field['url']=U('Index/Index/content',array('aid'=>\$field['aid']));
		\$field['title']=mb_substr(\$field['title'],0,$titlelen,'utf-8');
		?>
php;
	$php.=$content;
	$php.="<?php endforeach;?>";
	return $php;
	}

	//显示页码
	public function _pageshow(){
		return "<?php echo \$page->show();?>";
	}
}

?>